package com.syx.migration.service.impl;

import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.syx.migration.domain.*;
import com.syx.migration.mapper.*;
import com.syx.migration.mapper.old.OldBasicInfoMapper;
import com.syx.migration.service.IBasicInfoModuleService;
import com.syx.migration.service.MgnLinkIdService;
import com.syx.migration.utils.BaseUtils;
import com.syx.migration.utils.BeanMapUtils;
import com.syx.migration.utils.TimeIdWorker;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.*;

@RequiredArgsConstructor
@Service
public class BasicInfoModuleServiceImpl implements IBasicInfoModuleService {

    private final MgnLinkIdService mgnLinkIdService;

    private final SyxNoticeMapper noticeMapper;
    private final SyxSchoolMapper syxSchoolMapper;
    private final MgnLinkIdMapper mgnLinkIdMapper;
    private final OldBasicInfoMapper oldBasicInfoMapper;
    private final SyxPreviewRoomMapper previewRoomMapper;
    private final SyxLibraryRoomMapper libraryRoomMapper;
    private final SyxLibraryBlurbMapper libraryBlurbMapper;
    private final SyxLibraryRegimeMapper libraryRegimeMapper;
    private final SyxLibraryBookselfMapper libraryBookselfMapper;

    private static final String BELONG = "aafde3efa746480ab7652b3be493dfc0";

    /**
     * 同步学校
     */
    @Override
    @DSTransactional
    public void migrateSchool() {
        ArrayList<MgnLinkId> mgnLinkIds = new ArrayList<>();
        String tbName = "syx_school";
        final int[] code = {2};
        try{
            List<SyxSchool> schools = BeanMapUtils.mapListToBeanList(oldBasicInfoMapper.querySchool(), SyxSchool.class);
            schools.forEach(item ->{
                if(!item.getSchoolName().isEmpty() && item.getSchoolName().contains("凯瑞中学")){
                    item.setSchoolCode(String.format("%03d", 1));
                    mgnLinkIds.add(new MgnLinkId(String.format("%06d", 1), item.getBelong(), tbName));
                } else {
                    item.setSchoolCode(String.format("%03d", code[0]));
                    mgnLinkIds.add(new MgnLinkId(String.format("%06d", code[0]), item.getBelong(), tbName));
                    code[0] +=1;
                }
                item.setSchoolId(BaseUtils.str2Long(TimeIdWorker.createId(item.getCreateTime())));
                item.setSchoolProp("0".equals(item.getSchoolProp())?"1":"0");
            });
            syxSchoolMapper.insertBatch(schools);
            mgnLinkIdMapper.insertBatch(mgnLinkIds);
        } catch (Exception e){
            throw new RuntimeException(e);
        }

    }

    /**
     * 同步图书馆简介
     */
    @Override
    @DSTransactional
    public void migrateBlurb() {
        String tbName = "syx_library_blurb";
        ArrayList<MgnLinkId> mgnLinkIds = new ArrayList<>();
        Map<String, String> schoolMap = mgnLinkIdService.getMapByTbName("syx_school");
        List<SyxLibraryBlurb> blurbs = BeanMapUtils.mapListToBeanList(oldBasicInfoMapper.queryBlurb(BELONG), SyxLibraryBlurb.class);
        blurbs.forEach(item ->{
            String newId = TimeIdWorker.createId(item.getCreateTime());
            item.setId(BaseUtils.str2Long(newId));
            String tenantId = schoolMap.get(item.getBelong());
            item.setTenantId(tenantId);
            mgnLinkIds.add(new MgnLinkId(newId, item.getOldId(), tbName));
        });
        System.out.println(blurbs);
        System.out.println(mgnLinkIds);
        libraryBlurbMapper.insertBatch(blurbs);
        mgnLinkIdMapper.delete(new LambdaQueryWrapper<MgnLinkId>().eq(MgnLinkId::getTableName, tbName));
        mgnLinkIdMapper.insertBatch(mgnLinkIds);
    }

    /**
     * 同步公告
     */
    @Override
    @DSTransactional
    public void migrateNotice() {
        String tbName = "syx_notice";
        ArrayList<MgnLinkId> mgnLinkIds = new ArrayList<>();
        Map<String, String> schoolMap = mgnLinkIdService.getMapByTbName("syx_school");
        List<SyxNotice> notices = BeanMapUtils.mapListToBeanList(oldBasicInfoMapper.queryNotice(BELONG), SyxNotice.class);
        notices.forEach(item ->{
            String newId = TimeIdWorker.createId(item.getCreateTime());
            item.setId(BaseUtils.str2Long(newId));
            String tenantId = schoolMap.get(item.getBelong());
            item.setTenantId(tenantId);
            mgnLinkIds.add(new MgnLinkId(newId, item.getOldId(), tbName));
        });
        noticeMapper.insertBatch(notices);
        mgnLinkIdMapper.delete(new LambdaQueryWrapper<MgnLinkId>().eq(MgnLinkId::getTableName, tbName));
        mgnLinkIdMapper.insertBatch(mgnLinkIds);
    }

    /**
     * 同步规则(规章制度)
     */
    @Override
    @DSTransactional
    public void migrateRegime() {
        String tbName = "syx_library_regime";
        ArrayList<MgnLinkId> mgnLinkIds = new ArrayList<>();
        Map<String, String> schoolMap = mgnLinkIdService.getMapByTbName("syx_school");
        List<SyxLibraryRegime> regimes = BeanMapUtils.mapListToBeanList(oldBasicInfoMapper.queryRegime(BELONG), SyxLibraryRegime.class);
        regimes.forEach(item ->{
            String newId = TimeIdWorker.createId(item.getCreateTime());
            item.setId(BaseUtils.str2Long(newId));
            String tenantId = schoolMap.get(item.getBelong());
            item.setTenantId(tenantId);
            mgnLinkIds.add(new MgnLinkId(newId, item.getOldId(), tbName));
        });
        libraryRegimeMapper.insertBatch(regimes);
        mgnLinkIdMapper.delete(new LambdaQueryWrapper<MgnLinkId>().eq(MgnLinkId::getTableName, tbName));
        mgnLinkIdMapper.insertBatch(mgnLinkIds);
    }

    /**
     * 同步预览室
     */
    @Override
    @DSTransactional
    public void migratePreviewRoom() {
        String tbName = "syx_preview_room";
        ArrayList<MgnLinkId> mgnLinkIds = new ArrayList<>();
        Map<String, String> schoolMap = mgnLinkIdService.getMapByTbName("syx_school");
        List<SyxPreviewRoom> previewRooms = BeanMapUtils.mapListToBeanList(oldBasicInfoMapper.queryPreviewRoom(BELONG), SyxPreviewRoom.class);
        previewRooms.forEach(item ->{
            String newId = TimeIdWorker.createId(item.getCreateTime());
            item.setId(BaseUtils.str2Long(newId));
            String tenantId = schoolMap.get(item.getBelong());
            item.setTenantId(tenantId);
            mgnLinkIds.add(new MgnLinkId(newId, item.getOldId(), tbName));
        });
        previewRoomMapper.insertBatch(previewRooms);
        mgnLinkIdMapper.delete(new LambdaQueryWrapper<MgnLinkId>().eq(MgnLinkId::getTableName, tbName));
        mgnLinkIdMapper.insertBatch(mgnLinkIds);
    }

    /**
     * 同步藏书室
     */
    @Override
    @DSTransactional
    public void migrateLibraryRoom() {
        String tbName = "syx_library_room";
        ArrayList<MgnLinkId> mgnLinkIds = new ArrayList<>();
        Map<String, String> schoolMap = mgnLinkIdService.getMapByTbName("syx_school");
        List<SyxLibraryRoom> libraryRooms = BeanMapUtils.mapListToBeanList(oldBasicInfoMapper.queryLibraryRoom(BELONG), SyxLibraryRoom.class);
        libraryRooms.forEach(item ->{
            String newId = TimeIdWorker.createId(item.getCreateTime());
            item.setId(BaseUtils.str2Long(newId));
            String tenantId = schoolMap.get(item.getBelong());
            item.setTenantId(tenantId);
            item.setIsEnable("true".equals(item.getIsEnable())?"0":"1");
            item.setIsCatalog("0".equals(item.getIsCatalog())?"1":"0");
            mgnLinkIds.add(new MgnLinkId(newId, item.getOldId(), tbName));
        });
        libraryRoomMapper.insertBatch(libraryRooms);
        mgnLinkIdMapper.delete(new LambdaQueryWrapper<MgnLinkId>().eq(MgnLinkId::getTableName, tbName));
        mgnLinkIdMapper.insertBatch(mgnLinkIds);
    }

    @Override
    @DSTransactional
    public void migrateLibraryBookself() {
        String tbName = "syx_library_bookself";
        ArrayList<MgnLinkId> mgnLinkIds = new ArrayList<>();
        Map<String, String> schoolMap = mgnLinkIdService.getMapByTbName("syx_school");
        Map<String, String> libraryRoomMap = mgnLinkIdService.getMapByTbName("syx_library_room");
        List<Map<String, Object>> bookSelfList = oldBasicInfoMapper.queryLibraryBookself(BELONG);
        bookSelfList.forEach(item->{
            String roomId1 = item.get("roomId").toString();
            String roomId = libraryRoomMap.get(roomId1);
            item.put("roomId", roomId);
        });
        List<SyxLibraryBookself> libraryBookselfs = BeanMapUtils.mapListToBeanList(bookSelfList, SyxLibraryBookself.class);
        libraryBookselfs.forEach(item ->{
            String newId = TimeIdWorker.createId(item.getCreateTime());
            item.setBookselfId(BaseUtils.str2Long(newId));
            String tenantId = schoolMap.get(item.getBelong());
            item.setTenantId(tenantId);
            mgnLinkIds.add(new MgnLinkId(newId, item.getOldId(), tbName));
        });
        libraryBookselfMapper.insertBatch(libraryBookselfs);
        mgnLinkIdMapper.delete(new LambdaQueryWrapper<MgnLinkId>().eq(MgnLinkId::getTableName, tbName));
        mgnLinkIdMapper.insertBatch(mgnLinkIds);
    }

}
